
# Set up the packages, options, and working directory path
pacman::p_load(tidyverse, fixest)
set.seed(42)
options(scipen=999)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
}


##
# Set up the data and code for the regression output matrix
##

# Set up the dataset for selection
ctcl2020 = haven::read_dta(file.path(root, "/modified_data/ctcl.dta")) %>%
  filter(policy_year==2020) %>%
  mutate(pop_terc = ceiling(percent_rank(vap)*3),
    pop_terc = ifelse(pop_terc==0, 1, pop_terc))

# Make a function for extracting regression output
reg_output_matrix = function(regs, coef_names){
  num_regs = length(regs)
  num_coefs = length(coef_names)
  out = matrix(nrow=num_coefs*2 + 1, ncol=num_regs)
  for(r in 1:num_regs){
    for(c in 1:num_coefs){
      out[c*2 - 1, r] = coefficients(regs[[r]])[coef_names[c]]
      out[c*2, r] = se(regs[[r]])[coef_names[c]]
    }
    out[num_coefs*2 + 1, r] = regs[[r]]$nobs
  }
  return(out)
}


##
# Make main selection table
##

# Run the selection regressions
selec1 = feols(treat ~ lag_vs_dem4_pres, data=ctcl2020, se="hetero")
selec2 = feols(treat ~ lag_vs_dem4_pres | state, data=ctcl2020, se="hetero")
selec3 = feols(treat ~ lag_vs_dem4_pres + log(vap) + log(med_income2019) | state, data=ctcl2020, se="hetero")
selec4 = feols(treat ~ lag_vs_dem4_pres + log(vap) + log(med_income2019) + metro + share_white_nh | state, data=ctcl2020, se="hetero")
selec5 = feols(treat ~ lag_vs_dem4_pres + log(vap) + log(med_income2019) + metro + share_white_nh + covid_death_rate + soc_dist | state, data=ctcl2020, se="hetero")
selec6 = feols(treat ~ lag_vs_dem4_pres + log(vap) + log(med_income2019) + metro + share_white_nh + covid_death_rate + soc_dist + naco | state, data=ctcl2020, se="hetero")
selec7 = feols(treat ~ lag_vs_dem4_pres + log(vap) + log(med_income2019) + metro + share_white_nh + covid_death_rate + soc_dist + naco + dem_clerk | state, data=ctcl2020, se="hetero")

# Put the regression output into a matrix
regs = list(selec1, selec2, selec3, selec4, selec5, selec6, selec7)
coef_names = c("(Intercept)", "lag_vs_dem4_pres", "log(vap)", 
  "log(med_income2019)", "metro", "share_white_nh",
  "covid_death_rate", "soc_dist", "naco", "dem_clerk")
out = reg_output_matrix(regs, coef_names)

# Put the selection regression estimates in a tex table for the paper
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{Unpacking the Relationship Between Democratic Vote Share and Grant Application.}",
"\\label{tab:selection}}",
"\\begin{tabular}{lccccccc}",
"\\toprule \\toprule",
" & \\multicolumn{7}{c}{Applied for and Received Grant} \\\\",
" & (1) & (2) & (3) & (4) & (5) & (6) & (7) \\\\",
"\\midrule",
sprintf("Lag Dem Vote Share & %1.2f & %1.2f & %1.2f & %1.2f & %1.2f & %1.2f & %1.2f \\\\",
  out[3,1], out[3,2], out[3,3], out[3,4], out[3,5], out[3,6], out[3,7]),
sprintf(" & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[4,1], out[4,2], out[4,3], out[4,4], out[4,5], out[4,6], out[4,7]),
sprintf("Log(Population) & & & %1.2f & %1.2f & %1.2f & %1.2f & %1.2f \\\\",
  out[5,3], out[5,4], out[5,5], out[5,6], out[5,7]),
sprintf(" & & & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[6,3], out[6,4], out[6,5], out[6,6], out[6,7]),
sprintf("Log(Median Income) & & & %1.2f & %1.2f & %1.2f & %1.2f & %1.2f \\\\",
  out[7,3], out[7,4], out[7,5], out[7,6], out[7,7]),
sprintf(" & & & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[8,3], out[8,4], out[8,5], out[8,6], out[8,6]),
sprintf("Metro & & & & %1.2f & %1.2f & %1.2f & %1.2f \\\\",
  out[9,4], out[9,5], out[9,6], out[9,7]),
sprintf(" & & & & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[10,4], out[10,5], out[10,6], out[10,7]),
sprintf("Non-Hisp White Share & & & & %1.2f & %1.2f & %1.2f & %1.2f \\\\",
  out[11,4], out[11,5], out[11,6], out[11,6]),
sprintf(" & & & & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[12,4], out[12,5], out[12,6], out[12,7]),
sprintf("COVID Death Rate & & & & & %1.2f & %1.2f & %1.2f \\\\",
  out[13,5], out[13,6], out[13,7]),
sprintf(" & & & & & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[14,5], out[14,6], out[14,7]),
sprintf("Social Distancing Share & & & & & %1.2f & %1.2f & %1.2f \\\\",
  out[15,5], out[15,6], out[15,7]),
sprintf(" & & & & & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[16,5], out[16,6], out[16,7]),
sprintf("NACo & & & & & & %1.2f & %1.2f \\\\",
  out[17,6], out[17,7]),
sprintf(" & & & & & & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[18,6], out[18,7]),
sprintf("Dem Clerk & & & & & & & %1.2f \\\\",
  out[19,7]),
sprintf(" & & & & & & & (%1.2f) \\\\[2mm]",
  out[20,7]),
sprintf("Constant & %1.2f & & & & & \\\\",
  out[1,1]),
sprintf(" & (%1.2f) & & & & & \\\\[2mm]",
  out[2,1]),
sprintf("Observations & %4.0f & %4.0f & %4.0f & %4.0f & %4.0f & %4.0f & %4.0f \\\\",
  out[21,1], out[21,2], out[21,3], out[21,4], out[21,5], out[21,6], out[21,7]),
"\\midrule",
"State FEs & No & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{8}{p{0.95\\textwidth}}{\\footnotesize Robust standard reported in parentheses. ",
"Population is the voting-age population. ",
"Median Income is median household income measured with the 5-year ACS ending in 2019. ",
"Metro is an indicator for urban and suburban counties based on the Census nine-value urban-rural continuum. ",
"Non-Hisp White Share is the share of residents who are classified as non-Hispanic white in the 2020 census. ",
"Covid death rate is the number of deaths per 1,000 residents prior to September 1, 2020. ",
"Social Distancing Share is the share of Nationscape respondents in the county who report always complying ",
"with recommended social distancing in the early fall of 2020. ",
"NACo is an indicator for county membership in the National Association of Counties. ",
"Dem clerk is an indicator for a county having a Democratic local election official---limited to partisan officials.",
"}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/selection_table_alt.tex"), sep="\n")


##
# Make Vote-by-Mail Selection Table
##

# Set up the dataset for selection
uvbm_states = c("WA", "CO", "OR", "HI", "UT")
ctcl2020 = haven::read_dta(file.path(root, "/modified_data/ctcl.dta")) %>%
  filter(policy_year==2020) %>%
  mutate(pop_terc = ceiling(percent_rank(vap)*3),
    pop_terc = ifelse(pop_terc==0, 1, pop_terc),
    vbm_change = ifelse(state %in% uvbm_states, 0, no_excuse),
    vbm_change_alt = ifelse(state %in% uvbm_states, 0,
      ifelse(no_excuse==1, 1, NA)))

# Run vote-by-mail selection regressions
selec1 = feols(vbm ~ lag_vs_dem4_pres, data=ctcl2020, se="hetero")
selec2 = feols(vbm ~ lag_vs_dem4_pres + 
  log(vap) + log(med_income2019) + share_white_nh, data=ctcl2020, se="hetero")
selec3 = feols(vbm ~ lag_vs_dem4_pres + 
  log(vap) + log(med_income2019) + share_white_nh | state, data=ctcl2020, se="hetero")
selec4 = feols(treat ~ lag_vs_dem4_pres*vbm_change, data=ctcl2020, se="hetero")
selec5 = feols(treat ~ lag_vs_dem4_pres*vbm_change + 
  log(vap) + log(med_income2019) + share_white_nh, data=ctcl2020, se="hetero")
selec6 = feols(treat ~ lag_vs_dem4_pres*vbm_change + 
  log(vap) + log(med_income2019) + share_white_nh | state, data=ctcl2020, se="hetero")

# Put the regression output into a matrix
regs = list(selec1, selec2, selec3, selec4, selec5, selec6)
coef_names = c("lag_vs_dem4_pres", "lag_vs_dem4_pres:vbm_change", "vbm_change")
out = reg_output_matrix(regs, coef_names)

# Put the selection regression estimates in a tex table for the paper
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{In Democratic-Leaning Counties, More People Intended to Vote-by-Mail and Officials Were More Likely to Apply for CTCL Grant.}",
"\\label{tab:selection_covid}}",
"\\begin{tabular}{lccc|ccc}",
"\\toprule \\toprule",
" & \\multicolumn{3}{c}{Mail Voting Share} & \\multicolumn{3}{c}{Received Grant} \\\\",
" & (1) & (2) & (3) & (4) & (5) & (6) \\\\",
"\\midrule",
sprintf("Lag Dem Vote Share & %1.2f & %1.2f & %1.2f & %1.2f & %1.2f & %1.2f \\\\",
  out[1,1], out[1,2], out[1,3], out[1,4], out[1,5], out[1,6]),
sprintf(" & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[2,1], out[2,2], out[2,3], out[2,4], out[2,5], out[2,6]),
sprintf("Lag Dem Vote Share $\\times$ & & & & %1.2f & %1.2f & %1.2f \\\\",
  out[3,4], out[3,5], out[3,6]),
sprintf(" \\ \\ Mail Voting Expansion & & & & (%1.2f) & (%1.2f) & (%1.2f) \\\\[2mm]",
  out[4,4], out[4,5], out[4,6]),
sprintf("Mail Voting Expansion & & & & %1.2f & %1.2f & \\\\",
  out[5,4], out[5,5]),
sprintf(" & & & & (%1.2f) & (%1.2f) &  \\\\[2mm]",
  out[6,4], out[6,5]),
sprintf("Observations & %4.0f & %4.0f & %4.0f & %4.0f & %4.0f & %4.0f \\\\",
  out[7,1], out[7,2], out[7,3], out[7,4], out[7,5], out[7,6]),
"\\midrule",
"Controls & No & Yes & Yes & No & Yes & Yes \\\\",
"State FEs & No & No & Yes & No & No & Yes \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{7}{p{0.8\\textwidth}}{\\footnotesize Robust standard reported in parentheses. ",
"Mail voting share is the weighted share pre-election NationScape respondents in a county who ",
"said they intended to vote by mail. No-excuse expansion codes states that enacted no-excuse ",
"absentee voting for the first time in 2020 as 1. States that already had universal mail ballot ",
"delivery or offered absentee voting only with an excuse in 2020 are coded as 0. All other states ",
"are held out of this analysis. The controls are the log of voting age population, the log of ",
"median household income, and the non-Hispanic white share of the population.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/covid_selection_table.tex"), sep="\n")

